package com.huatai.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.BeanUtil;
import com.huatai.manage.query.PageQuery;

import com.huatai.manage.mapper.TbTrialSubjectMapper;
import com.huatai.manage.entity.TbTrialSubjectEntity;
import com.huatai.manage.query.TrialIdPageQuery;
import com.huatai.manage.service.TbSpecialEffectService;
import com.huatai.manage.service.TbTrialLabelService;
import com.huatai.manage.service.TbTrialModelService;
import com.huatai.manage.service.TbTrialSubjectService;
import com.huatai.manage.utils.ComUtil;
import com.huatai.manage.vo.PageVo;
import com.huatai.manage.vo.TbTrialSubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 试验对象;(TbTrialSubjectsManage)表服务实现类
 *
 * @author luoxc
 * @since 2023-03-14 14:13:39
 */
@Service("tbTrialSubjectsManageService")
public class TbTrialSubjectServiceImpl extends ServiceImpl<TbTrialSubjectMapper, TbTrialSubjectEntity> implements TbTrialSubjectService {

	@Autowired
	private TbTrialSubjectMapper tbTrialSubjectMapper;

	@Autowired
	private TbTrialModelService tbTrialModelService;

	@Autowired
	private TbTrialLabelService tbTrialLabelService;

	@Autowired
	private TbSpecialEffectService tbSpecialEffectService;

	/**
	 * 分页查询数据列表
	 *
	 * @param pageQuery
	 * @return
	 */
	@Override
	public PageVo pageList(PageQuery<TbTrialSubjectEntity> pageQuery) {
		Page<TbTrialSubjectEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		List<TbTrialSubjectVo> tbTrialSubjectVos = tbTrialSubjectMapper.pageList(page, pageQuery.getData());
		if (tbTrialSubjectVos.isEmpty()) return null;
		PageVo pageVo = BeanUtil.copyProperties(page, PageVo.class);
		return pageVo.setRecords(tbTrialSubjectVos);
	}

	/**
	 * 根据试验对象id获取数据
	 *
	 * @param subjectId
	 * @return
	 */
	@Override
	public TbTrialSubjectVo getBySubjectId(String subjectId) {
		//tbTrialSubjectMapper.getBySubjectId(subjectId);
		return tbTrialSubjectMapper.getBySubjectId(subjectId);
	}


	/**
	 * 根据试验id获取试验对象列表
	 *
	 * @param trialId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getByTrialId(String trialId) {
		List<Map<String, Object>> results = new ArrayList<>();
		List<TbTrialSubjectVo> list = tbTrialSubjectMapper.getByTrialId(trialId);
		list.forEach(i -> {
			Map<String, Object> map = new LinkedHashMap<>();
			map.put("value", i.getSubjectId());
			map.put("label", i.getSubjectName());
			map.put("ws", i.getWsUrl());
			results.add(map);
		});
		return results;
	}

	/**
	 * 根据试验id获取试验对象列表
	 *
	 * @return
	 */
	@Override
	public List<TbTrialSubjectVo> listByTrialId(String trialId) {
		return tbTrialSubjectMapper.listByTrialId(trialId);
	}

	/**
	 * 根据试验id分页获取试验对象列表
	 *
	 * @return
	 */
	@Override
	public PageVo pageByTrialId(TrialIdPageQuery trialIdPageQuery) {
		Page<TbTrialSubjectEntity> page = new Page<>(trialIdPageQuery.getCurrent(), trialIdPageQuery.getSize());
		List<TbTrialSubjectVo> tbTrialSubjectVos = tbTrialSubjectMapper.pageByTrialId(page, trialIdPageQuery.getTrialId());
		if (tbTrialSubjectVos.isEmpty()) return null;
		PageVo pageVo = BeanUtil.copyProperties(page, PageVo.class);
		return pageVo.setRecords(tbTrialSubjectVos);
	}

	/**
	 * 查询所有数据
	 *
	 * @return
	 */
	@Override
	public List<TbTrialSubjectVo> subjectList() {
		return tbTrialSubjectMapper.subjectList();
	}

	/**
	 * 根据试验对象id修改数据
	 *
	 * @param tbTrialSubjectEntity
	 * @return
	 */
	@Override
	public boolean update(TbTrialSubjectEntity tbTrialSubjectEntity) {
		LambdaUpdateWrapper<TbTrialSubjectEntity> wrapper = Wrappers.<TbTrialSubjectEntity>lambdaUpdate();
		wrapper.eq(TbTrialSubjectEntity::getSubjectId, tbTrialSubjectEntity.getSubjectId());
		return this.update(tbTrialSubjectEntity, wrapper);
	}

	/**
	 * 根据试验对象id集合删除数据
	 *
	 * @param subjectIds
	 * @return
	 */
	@Override
	public boolean removes(List<String> subjectIds) {
		if (subjectIds.isEmpty()) return false;
		return remove(Wrappers.<TbTrialSubjectEntity>lambdaQuery()
			.in(TbTrialSubjectEntity::getSubjectId, subjectIds));
	}

	/**
	 * 新增模型对象数据
	 *
	 * @param tbTrialSubject
	 * @return
	 */
	@Override
	public boolean insert(TbTrialSubjectEntity tbTrialSubject) {
		if (isIdExist(tbTrialSubject)) {
			tbTrialSubject.setSubjectId(ComUtil.getUUID());
			this.save(tbTrialSubject);
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否id是否存在
	 *
	 * @param tbTrialSubject
	 * @return
	 */
	public boolean isIdExist(TbTrialSubjectEntity tbTrialSubject) {
		String modelId = tbTrialSubject.getModelId();
		String effectId = tbTrialSubject.getEffectId();
		String labelId = tbTrialSubject.getLabelId();
		if (tbTrialModelService.isExist(modelId)
			&& tbSpecialEffectService.isExist(effectId)
			&& tbTrialLabelService.isExist(labelId)) {
			return true;
		} else {
			return false;
		}
	}

}

